package com.duolebo.uteped_sdk.receiver;

/**
 * @Author: LiuYang
 * @Date: 2023/12/23 23:56
 * @Description:
 */

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Telephony;
import android.telephony.SmsMessage;
import android.text.TextUtils;
import android.util.Log;

import com.duolebo.uteped_sdk.UTEPedBleClientHelper;
import com.duolebo.uteped_sdk.utils.Const;
import com.duolebo.uteped_sdk.utils.DeviceHelper;
import com.yc.nadalsdk.bean.MessageInfo;
import com.yc.nadalsdk.constants.RemindType;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 短信拦截广播
 */

public class SmsReceiver extends BroadcastReceiver {
    public final static String TAG = "SmsRemindReceiver";
    public SmsMessage[] messages;
    public String phoneNumber = null;
    public String body;

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Bundle bundle = intent.getExtras();
        if (bundle == null || action == null)
            return;

        if (action.equals(Telephony.Sms.Intents.SMS_RECEIVED_ACTION)) {
            onReceiveSms(bundle);
        }
    }

    public void onReceiveSms(Bundle bundle) {
        Log.i(TAG, "onReceiveSms");

        try {
            Object[] pdus = (Object[]) bundle.get("pdus");
            if (pdus != null) {
                messages = new SmsMessage[pdus.length];  //解析短信
                for (int i = 0; i < pdus.length; i++) {
                    byte[] pdu = (byte[]) pdus[i];
                    messages[i] = SmsMessage.createFromPdu(pdu);
                }
            }

            SmsMessage sms = messages[0];
            //输出参数内容
            Date date = new Date(sms.getTimestampMillis());  // 获取短信日期
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
            String receiveTime = sdf.format(date);
            //日期
            String data = receiveTime.trim();
            //电话号码
            phoneNumber = sms.getOriginatingAddress();
            //Email姓名 没有为 null
            //String emailName = sms.getEmailBody();
            String title = null;
            if (sms.isEmail()) {
                title = sms.getEmailFrom();
            }

            //文本
            if (messages.length == 1) {
                body = sms.getDisplayMessageBody();
            } else {
                StringBuilder sb = new StringBuilder();
                for (SmsMessage message : messages) {
                    sms = message;
                    sb.append(sms.getDisplayMessageBody());
                }
                body = sb.toString();
            }

            if (TextUtils.isEmpty(title)) {
                title = phoneNumber;
            }

            MessageInfo msgInfo = new MessageInfo();
            msgInfo.setType(RemindType.TYPE_SMS);
//            msgInfo.setContact(phoneNumber);
//            msgInfo.setPhoneNumber(phoneNumber);
            msgInfo.setTitle(title);
            msgInfo.setContent(body);
            msgInfo.setVibrate(true);

            Log.d(TAG, "receive message title: " + title);

            boolean enable = DeviceHelper.getInstance().read(Const.M_setSmsRemindEnable,false);
            if (enable) {
                UTEPedBleClientHelper.shareInstance().sendMessage(msgInfo);
            }
            else {
                Log.d(TAG, "sms remind disabled");
            }
        } catch (Exception e) {
            Log.e(TAG, "SmsReceiver onReceive Exception: " + e.getMessage());
            e.printStackTrace();
        }
    }
}